import { Type, applyDecorators } from '@nestjs/common'
import { ApiOkResponse, getSchemaPath } from '@nestjs/swagger'
import { ResultVo } from 'src/vo/common/result.vo'


const baseTypeNames = ['String', 'Number', 'Boolean']
/**
 * 封装 swagger 返回统一结构
 * 支持复杂类型 {  code, message, data }
 * @param model 返回的 data 的数据类型
 * @param isArray data 是否是数组
 * @param isPager 设置为 true, 则 data 类型为 { list, total } , false data 类型是纯数组
 */
export const ApiResult = <TModel extends Type<any>>(model?: TModel, isArray?: boolean, isPager?: boolean) => {
  let items = null
  if (model && baseTypeNames.includes(model.name)) {
    items = { type: model.name.toLocaleLowerCase() }
  } else {
    items = { $ref: getSchemaPath(model) }
  }
  let prop = null
  if (isArray && isPager) {
    prop = {
      type: 'object',
      properties: {
        page: {
          type: 'number',
          default: 0
        },
        size: {
          type: 'number',
          default: 0
        },
        totalCount: {
          type: 'number',
          default: 0
        },
        data: {
          type: 'array',
          items
        },
      }
    }
  } else if (isArray) {
    prop = {
      type: 'array',
      items
    }
  } else if (model) {
    prop = items
  } else {
    prop = { type: 'null', default: null }
  }
  return applyDecorators(ApiOkResponse({
    schema: {
      allOf: [
        { $ref: getSchemaPath(ResultVo) },
        {
          properties: {
            code: {
              type: 'number',
              default: 200
            },
            message: {
              type: 'string',
              default: 'success'
            },
            data: prop
          }
        }
      ]
    }
  }))
}